// pages/addressDetail/addressDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    region: [],
    address: '',
    isDefault: false,
    id: ''
  },
  nameChange: function(e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  mobileChange: function(e) {
    this.setData({
      mobile: e.detail.value
    })
    console.log(this.data.mobile)
  },
  regionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
    console.log(this.data.region)
  },
  addressChange: function(e) {
    this.setData({
      address: e.detail.value
    })
    console.log(this.data.address)
  },
  switchChange: function(e) {
    this.setData({
      isDefault: e.detail.value
    })
    console.log(this.data.isDefault)
  },
  getDetail: function() {
    var _this = this
    wx.request({
      url: app.globalData.url+'/v1/address/details',
      method: 'GET',
      data: {
        id: this.data.id,
        openid: app.globalData.openId
      },
      success: function(r) {
        r = r.data
        if (r.code == 0) {
          _this.setData({
            name: r.data.name,
            mobile: r.data.mobile,
            region: [r.data.province, r.data.city, r.data.district],
            address: r.data.address,
            isDefault: r.data.is_default == 2
          })
        }
      }
    })
  },
  submit() {
    var _this = this
    wx.showToast({
      title: 'loading...',
      icon: 'loading',
      duration: 60000
    })
    wx.request({
      url: !_this.data.id ? app.globalData.url+'/v1/address/address' : app.globalData.url+'/v1/address/default',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        name: _this.data.name,
        mobile: _this.data.mobile,
        province: _this.data.region[0],
        city: _this.data.region[1],
        district: _this.data.region[2],
        address: _this.data.address,
        id: _this.data.id,
        openid: app.globalData.openId
      },
      success: function(r) {
        if (r.data.code == 0) {
          if (_this.data.isDefault) {
            _this.setDefault(r.data.data.id)
          } else {
            wx.hideToast()
            wx.showToast({
              title: '操作成功'
            })
            setTimeout(function(){
              wx.navigateBack()
            }, 1500)
          }
        } else {
          wx.hideToast()
          wx.showToast({
            title: r.data.data.message,
          })
        }
      }
    })
  },
  setDefault: function(id) {
    var _this = this
    wx.request({
      url: app.globalData.url+'/v1/address/modify',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: app.globalData.openId,
        id: id ? id : _this.data.id
      },
      success: function(r) {
        if(r.data.code == 0) {
          wx.hideToast()
          wx.showToast({
            title: '操作成功！'
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 1500)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id) { 
      //如果url有参数，则请求数据并赋值到input，并且提交表单时请求 '修改接口'
      this.setData({
        id: options.id
      })
      this.getDetail()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})